package com.leetcode.algorithm.y22.m09.w4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 2231. 按奇偶性交换后的最大数字
 * 
 * https://leetcode.cn/problems/largest-number-after-digit-swaps-by-parity/
 * 
 * @author jie.deng
 *
 */
class Question2231Solution01 {

	public int largestInteger(int num) {
		char[] chs = ("" + num).toCharArray();

		List<Integer> oddIdxList = new ArrayList<>();
		List<Character> oddChList = new ArrayList<>();
		List<Integer> evenIdxList = new ArrayList<>();
		List<Character> evenChList = new ArrayList<>();
		for (int i = 0; i < chs.length; i++) {
			char ch = chs[i];
			if (((ch - '0') & 1) == 1) {
				oddChList.add(ch);
				oddIdxList.add(i);
			} else {
				evenChList.add(ch);
				evenIdxList.add(i);
			}
		}

		Collections.sort(oddChList, Comparator.reverseOrder());
		for (int i = 0; i < oddChList.size(); i++) {
			chs[oddIdxList.get(i)] = oddChList.get(i);
		}

		Collections.sort(evenChList, Comparator.reverseOrder());
		for (int i = 0; i < evenChList.size(); i++) {
			chs[evenIdxList.get(i)] = evenChList.get(i);
		}
		return Integer.parseInt(new String(chs));
	}

}